import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qgchat/common/action/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/newanchorModel.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'Bloc/sameCityBloC.dart';

class AnchorCard extends StatefulWidget {
  AnchorCard({Key key}) : super(key: key);

  @override
  _AnchorCardState createState() => _AnchorCardState();
}

class _AnchorCardState extends State<AnchorCard> {
  @override
  void initState() {
    super.initState();
    BlocProvider.of<SameCityBloc>(context).add(SameCityEvent.refresh);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: EasyRefresh(
        header: EasyRefreshSetting.header,
        footer: EasyRefreshSetting.footer,
        onRefresh: () async {
          BlocProvider.of<SameCityBloc>(context).add(SameCityEvent.refresh);
        },
        onLoad: () async {
          BlocProvider.of<SameCityBloc>(context).add(SameCityEvent.load);
        },
        child: ListView(
          padding: EdgeInsets.all(px(30)),
          children: <Widget>[
            BlocBuilder<SameCityBloc, List<NewanchorModel>>(
              builder: (context, list) {
                return Column(
                  children: list.map<Widget>((f) {
                    return card(context, f);
                  }).toList(),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget card(context, NewanchorModel item) {
    return Card(
      margin: EdgeInsets.only(bottom: px(30)),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(px(12))),
      ),
      elevation: px(10),
      color: Colors.white,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(px(12)),
        child: Container(
          width: px(690),
          height: px(648),
          child: FlatButton(
            onPressed: () => goPage(context, '/anchorInfo',
                param: {'token': item.memberToken}),
            padding: EdgeInsets.zero,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    Container(
                      width: px(690),
                      height: px(518),
                      child: CachedNetworkImage(
                        imageUrl: "${item.firstimg}?imageView2/2/w/690",
                        fit: BoxFit.cover,
                        errorWidget: (context, url, error) {
                          return Config.errorNetImage;
                        },
                      ),
                    ),
                    Positioned(
                      top: px(15),
                      right: px(15),
                      child: Container(
                        height: px(30),
                        padding: EdgeInsets.symmetric(horizontal: px(10)),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(
                            Radius.circular(px(15)),
                          ),
                          color: Colors.black.withOpacity(.3),
                        ),
                        child: Row(
                          children: <Widget>[
                            Container(
                              width: px(8),
                              height: px(8),
                              margin: EdgeInsets.only(right: px(5)),
                              decoration: BoxDecoration(
                                color: CommonAction.checkAnchorStatus(
                                  item.disturb,
                                  item.online,
                                ).color,
                                borderRadius: BorderRadius.circular(
                                  px(4),
                                ),
                              ),
                            ),
                            Text(
                              "${CommonAction.checkAnchorStatus(
                                item.disturb,
                                item.online,
                              ).text}",
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: px(22),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Positioned(
                      bottom: px(15),
                      left: px(15),
                      child: Container(
                        child: Row(
                          children: <Widget>[
                            Container(
                              margin: EdgeInsets.only(right: px(7)),
                              child: Image.asset(
                                "assets/images/icon/money.png",
                                width: px(26),
                                height: px(26),
                              ),
                            ),
                            Text(
                              "${item.timePrice}金币/分钟",
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: px(24),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                Container(
                  margin: EdgeInsets.only(top: px(20)),
                  padding: EdgeInsets.symmetric(horizontal: px(25)),
                  child: Text(
                    "${item.memberNickname}",
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: col("303133"),
                      fontSize: px(32),
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: px(10)),
                  padding: EdgeInsets.symmetric(horizontal: px(25)),
                  child: Text(
                    "${item.anchorSign}",
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: col("909399"),
                      fontSize: px(26),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
